// pages/monthReportEdit/monthReportEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    level:[],
    level_selected:'',
    radio:'通过',
    audit_popup_show:false,
    monthCollocation:[],
    now_month_text:'',
    now_year:'',
    now_month:'',
    steps: [],
    active:0
  },

/**切换年份 */
changeMonth(e){
  let type = e.currentTarget.dataset.type;
  let month = '';
  let year = this.data.now_year
  console.log(this.data.now_year);
  if(type=='pre'){
    month=(Number(year)-1)+'-12';
  }else if(type=='next'){
    month=(Number(year)+1)+'-01';
  }
  //请求数据
  this.requstData(month,'')
},
  /**
   * 月份点击事件
   */
  monthClick:function(e){
    this.requstData(e.currentTarget.dataset.month)
  },
 
  edit(){
    wx.navigateTo({
      url: '../monthReportEdit/monthReportEdit?id='+this.data.info.id
    })
  
  },
  /**提交审核 */
  commitAudit(){
    let id = this.data.info.id;
    api.curlPost('?cla=workReportApp&fun=commitAudit',{id:id,page:'workReportMonth'}).then((res)=>{
      wx.navigateTo({
        url: '../workReport/workReport'
      })
    })
  },
  /**审阅 */
  audit(){
    let id = this.data.info.id;
    let that = this;
    api.curlPost('?cla=workReportApp&fun=getAuditPage',{id:id,page:'workReportMonth'}).then((res)=>{
      that.setData({
        audit_popup_show:true,
        level:res.data.level
      })
    })
  },
  cancel(){
    this.setData({
      audit_popup_show:false
    })
  },
  /**审核 */
  auditComplete(e){
    let id = this.data.info.id;
    let para = e.detail.value;
    para.id = id;
    api.curlPost('?cla=workReportApp&fun=review',para).then((res)=>{
      wx.navigateTo({
        url: '../workReport/workReport',
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(api.isBlank(options.id)){
      wx.showToast({
        title: '未找到ID',
        icon: 'none',
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 1,
        })
      },3000)
   } else{
    this.requstData('',options.id)
   }
  
  },
  requstData(month='',id=''){
    let that = this;
    api.curlPost('?cla=workReportApp&fun=detail',{type:'月报',id:id,month:month}).then((res)=>{
        that.setData({
          info:res.data.result,
          monthCollocation:res.data.result.dayRecord,
          now_month:res.data.result.now_month,
          now_year:res.data.result.now_year,
          now_month_text:api.formatDateCRM('Y年',res.data.result.now_month),
          fileList:res.data.result.attachFile?res.data.result.attachFile:[],
          steps:res.data.result.auditRecord,
          active:res.data.result.active
        }) 
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})